import os
import conf
from incl import *
from data_objects import *
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class MainPage(webapp.RequestHandler):
  def get(self):

    user = users.GetCurrentUser()
    profile = None
    
    if user:
      profile = UserProfiles.load(user.email())
      if not profile:
        self.redirect('/profile/new')
      
      url = users.create_logout_url(self.request.uri)
      url_linktext = 'Logout'
    else:
      usr_cnt = 0
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'

    recipes_query = Recipes.all().order('-pub_date');
    recipes = recipes_query.fetch(10)
    
    template_values = {
      'config': conf,
      'profile': profile,
      'SectionName': "Home", 
      'ProjectLogo': "<a href='/'><div id='logo'>FoodExpert</div></a>",
      'recipes': recipes,
      'usernickname': users.get_current_user(),
      'searchbar': search_bar(self),
      'userbar': user_bar(self),
      'is_user_admin': users.is_current_user_admin(),
      'url': url,
      'url_linktext': url_linktext,
    }
    
    path = os.path.join(os.path.dirname(__file__), 'home.html')
    self.response.out.write(template.render(path, template_values))
    
class RecipeBook(webapp.RequestHandler):
  def post(self):
    
    recipe = Recipes(author=users.get_current_user(), 
                     name = "nobody")

    if users.get_current_user():
      recipe.author = users.get_current_user()
    
    recipe.name = self.request.get('name')
    recipe.descript = self.request.get('descript')
    recipe.put()
    
    self.redirect('/fe/')

class SearchEngine(webapp.RequestHandler):
  def post(self):
    self.redirect('/fe/')


class RecipePage(webapp.RequestHandler):
  def get(self):

    user = users.GetCurrentUser()
    profile = None

    if user:
      profile = UserProfiles.load(user.email())
      if not profile:
        self.redirect('/profile/new')

      url = users.create_logout_url(self.request.uri)
      url_linktext = 'Logout'
    else:
      usr_cnt = 0
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'


    rName = self.request.get("name", default_value="")
    recipe = Recipes.getByName(rName)

    tval = {
      'config': conf,
      'profile': profile,
      'SectionName': ""+recipe.name,
      'ProjectLogo': "<a href='/'><div id='logo'>FoodExpert</div></a>",
      'SectionKeywords': ""+recipe.name,
      'recipe': recipe,
      'usernickname': users.get_current_user(),
      'searchbar': search_bar(self),
      'userbar': user_bar(self),
      'is_user_admin': users.is_current_user_admin(),
      'url': url,
      'url_linktext': url_linktext,
    }

    path = os.path.join(os.path.dirname(__file__), 'recipe.html')
    self.response.out.write(template.render(path, tval))

def main():
  application = webapp.WSGIApplication(
    [('/fe/', MainPage),
     ('/recipe/', RecipePage),
     ('/post', RecipeBook),
     ('/se', SearchEngine),
     ('/se/', SearchEngine),
    ], debug=True)
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
